﻿namespace UnityEditor.Graphs.AnimationStateMachine
{
    using System;
    using UnityEditor;
    using UnityEditor.Graphs;
    using UnityEditorInternal;
    using UnityEngine;

    [CanEditMultipleObjects, CustomEditor(typeof(UnityEditorInternal.State))]
    internal class StateEditor : Editor
    {
        private SerializedProperty m_FootIK;
        private SerializedProperty m_Motions;
        private SerializedProperty m_Name;
        private SerializedProperty m_Speed;
        private StateTransitionsEditor m_StateTransitionsEditor;
        private SerializedProperty m_Tag;

        public override bool HasPreviewGUI()
        {
            return ((this.m_StateTransitionsEditor != null) && this.m_StateTransitionsEditor.HasPreviewGUI());
        }

        private void Init()
        {
            if (this.m_StateTransitionsEditor == null)
            {
                this.m_StateTransitionsEditor = new StateTransitionsEditor(this.state, this);
            }
        }

        public void OnDestroy()
        {
            this.m_StateTransitionsEditor.OnDestroy();
        }

        public void OnDisable()
        {
            this.m_StateTransitionsEditor.OnDisable();
        }

        public void OnEnable()
        {
            this.Init();
            this.m_Name = base.serializedObject.FindProperty("m_Name");
            this.m_Speed = base.serializedObject.FindProperty("m_Speed");
            this.m_Motions = base.serializedObject.FindProperty("m_Motions");
            this.m_FootIK = base.serializedObject.FindProperty("m_IKOnFeet");
            this.m_Tag = base.serializedObject.FindProperty("m_Tag");
            this.m_StateTransitionsEditor.OnEnable();
        }

        internal override void OnHeaderControlsGUI()
        {
            base.serializedObject.Update();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(30f) };
            GUILayout.Label("Tag", options);
            GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.MinWidth(0f) };
            EditorGUILayout.PropertyField(this.m_Tag, GUIContent.none, optionArray2);
            GUILayout.Space(34f);
            GUILayout.EndHorizontal();
            base.serializedObject.ApplyModifiedProperties();
        }

        internal override void OnHeaderIconGUI(Rect iconRect)
        {
            Texture2D miniThumbnail = AssetPreview.GetMiniThumbnail(base.target);
            GUI.Label(iconRect, miniThumbnail);
        }

        internal override void OnHeaderTitleGUI(Rect titleRect, string header)
        {
            base.serializedObject.Update();
            Rect position = titleRect;
            position.height = 16f;
            EditorGUI.BeginChangeCheck();
            EditorGUI.showMixedValue = this.m_Name.hasMultipleDifferentValues;
            string str = EditorGUI.DelayedTextField(position, this.m_Name.stringValue, null, EditorStyles.textField);
            EditorGUI.showMixedValue = false;
            if ((EditorGUI.EndChangeCheck() && !string.IsNullOrEmpty(str)) && (str != this.m_Name.stringValue))
            {
                foreach (UnityEngine.Object obj2 in base.targets)
                {
                    string name = this.state.GetStateMachine().MakeUniqueStateName(str);
                    ObjectNames.SetNameSmart(obj2, name);
                }
            }
            base.serializedObject.ApplyModifiedProperties();
        }

        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            EditorGUILayout.PropertyField(this.m_Speed, new GUILayoutOption[0]);
            if (!base.serializedObject.isEditingMultipleObjects)
            {
                Motion objectReferenceValue = this.m_Motions.GetArrayElementAtIndex(AnimatorControllerTool.tool.motionSetIndex).objectReferenceValue as Motion;
                EditorGUI.BeginChangeCheck();
                objectReferenceValue = EditorGUILayout.ObjectField("Motion", objectReferenceValue, typeof(Motion), true, new GUILayoutOption[0]) as Motion;
                if (EditorGUI.EndChangeCheck())
                {
                    this.m_Motions.GetArrayElementAtIndex(AnimatorControllerTool.tool.motionSetIndex).objectReferenceValue = objectReferenceValue;
                }
            }
            EditorGUILayout.PropertyField(this.m_FootIK, GUIContent.Temp("Foot IK"), new GUILayoutOption[0]);
            if (!base.serializedObject.isEditingMultipleObjects)
            {
                this.m_StateTransitionsEditor.OnInspectorGUI();
            }
            base.serializedObject.ApplyModifiedProperties();
        }

        public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
        {
            if (this.m_StateTransitionsEditor != null)
            {
                this.m_StateTransitionsEditor.OnInteractivePreviewGUI(r, background);
            }
        }

        public override void OnPreviewSettings()
        {
            if (this.m_StateTransitionsEditor != null)
            {
                this.m_StateTransitionsEditor.OnPreviewSettings();
            }
        }

        private UnityEditorInternal.State state
        {
            get
            {
                return (base.target as UnityEditorInternal.State);
            }
        }
    }
}

